$(function(){
	loadUpdatePersonData();
	
	$("#alterBut").click(function(){
		$("#alterDiv").hide();
	});
	loadAlter();
})

function showUpdatePersonData(){
	//修改个人资料
	$("#UpdatePersonDataDiv").show();
	$("#UpateMyPwdDiv").hide();
	$("#insertCompanyDataDiv").hide();
	$("#updateCompanyDataDiv").hide();
	
	$("#top_font").text("修改个人资料");
	loadUpdatePersonData();
}
function loadUpdatePersonData(){
	utilAjax("/recruiter/queryByRecruiter_id","","post",
			function(data){
		$("#fileImg").attr("src","/image/"+data.recruiter_img);
		$("#updteRecruiter_name").val(data.recruiter_name);
		$("#updteRecruiter_phone").val(data.recruiter_phone);
		$("#updteRecruiter_card").val(data.recruiter_card);
		$("#updteRecruiter_email").val(data.recruiter_email);
		$("#updteRecruiter_msg").val(data.recruiter_msg);
	});
}

function showUpateMyPwd(){
	//修改密码
	$("#UpateMyPwdDiv").show();
	$("#UpdatePersonDataDiv").hide();
	$("#insertCompanyDataDiv").hide();
	$("#updateCompanyDataDiv").hide();
	$("#top_font").text("修改密码");
}

function showInsertCompanyData(){
	//补充公司资料
	$("#insertCompanyDataDiv").show();
	$("#UpateMyPwdDiv").hide();
	$("#UpdatePersonDataDiv").hide();
	$("#updateCompanyDataDiv").hide();
	$("#top_font").text("补充公司资料");
}

function showUpdateCompanyData(){
	//修改公司资料
	$("#insertCompanyDataDiv").hide();
	$("#UpateMyPwdDiv").hide();
	$("#UpdatePersonDataDiv").hide();
	$("#updateCompanyDataDiv").show();
	$("#top_font").text("修改公司资料");
	loadqueryByCompany();
}
function loadqueryByCompany(){
	utilAjax("/company/queryByCompany_id","","post",
			function(data){
		$("#updateCompany_id").val(data.company_id);
		$("#updateCompany_name").val(data.company_name);
		$("#updateCompany_img").attr("src","/image/"+data.company_img);
		$("#updateCompany_address").val(data.company_address);
		$("#updateCompany_phone").val(data.company_phone);
		$("#updateCompany_introduce").val(data.company_introduce);
	});
}

var imgurl = "";
function getImgURL(node) {	
	var imgURL = "";	
    try{
        var file = null;
        if(node.files && node.files[0] ){
        	file = node.files[0]; 
        }else if(node.files && node.files.item(0)) {                    			
        	file = node.files.item(0);   
        } 	
        try{
			imgURL =  file.getAsDataURL();  
        }catch(e){
        	imgRUL = window.URL.createObjectURL(file);
        }
     }catch(e){     		
        if (node.files && node.files[0]) {                    		
        	var reader = new FileReader(); 
        	reader.onload = function (e) {                      	        	
        	    imgURL = e.target.result;  
        	};
        	reader.readAsDataURL(node.files[0]); 
        }  
     }
	creatImg(imgRUL);
	return imgURL;
}
function creatImg(imgRUL){   //根据指定URL创建一个Img对象
	$("#fileImg").attr("src",imgRUL);
}

function loadAlter(){
	var alterCon = $("#alterContent").text();
	if(alterCon!=""){
		$(".alterDiv").show();
		var interval = setInterval(function(){
			$(".alterDiv").hide();
			$("#alterContent").text("");
			clearInterval(interval);
		},3000);
	}
	$("#alterBut").click(function(){
		$(".alterDiv").hide();
	});
}

function utilAjax(url,data,type,success){
	$.ajax({
		url:url,
		data:data,
		dataType:'json',
		type:type,
		success:success,
		error:function(){
		}
	});
}

function checkFileImg(){
	//判断文本域是否有值  并判断是否是图片
	var myFile = $('#myFile').get(0).files[0];
	var reader = new FileReader();// 操作图片
	
	if(myFile==undefined){
		$('#myFile').css("border","1px solid red");
		$("#alterContent").text("请选择头像！！！");
		loadAlter();
		return false;
	}else{
		var imageType = $('#myFile').get(0).files[0].type;
		if(imageType.substring(0,imageType.indexOf("/"))!="image"){
			$('#myFile').css("border","1px solid red");
			$("#alterContent").text("请选择图片类型的文件！！！");
			loadAlter();
			return false;
		}else{
			 $("#myFile").css("border", "");
			return true;
		}
	}
	return false;	
}
function checkRecruiter_phone(){
	//判断手机号
	var checkRecruiter_phone = $("#checkRecruiter_phone").val();
	var reg_tel = /^1(3|4|5|6|7|8|9)\d{9}$/;//手机格式的正则
	 // 3.判断
    var flag = reg_tel.test(checkRecruiter_phone);
    if (flag) {
        $("#checkRecruiter_phone").css("border", "");
    } else {
        $("#checkRecruiter_phone").css("border", "1px solid red");
        $("#alterContent").html("请检查手机格式是否正确！！！");
		loadAlter();
    }
    // 4.返回校验是否通过
    return flag;
}
function checkUpdteRecruiter_email(){
	//判断邮箱
	var updteRecruiter_email = $("#updteRecruiter_email").val();
	//2.定义正则  itcast@163.com
    var reg_tel = /^\w+@\w+\.\w+$/;
    var flag = reg_tel.test(updteRecruiter_email);
    if (flag) {
        $("#updteRecruiter_email").css("border", "");
    } else {
        $("#updteRecruiter_email").css("border", "1px solid red");
        $("#alterContent").html("请检查邮箱格式是否正确！！！");
		loadAlter();
    }
    // 4.返回校验是否通过
    return flag;
}
function checkUpdteRecruiter_card(){
	//判断身份证
	var updteRecruiter_card = $("#updteRecruiter_card").val();
	var reg_tel = /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}[0-9Xx]$)/;
	var flag = reg_tel.test(updteRecruiter_card);
    if (flag) {
        $("#updteRecruiter_card").css("border", "");
    } else {
        $("#updteRecruiter_card").css("border", "1px solid red");
        $("#alterContent").html("请检查身份证格式是否正确！！！");
		loadAlter();
    }
    // 4.返回校验是否通过
    return flag;
}
function checkAll(){
	//修改个人资料
	if(checkFileImg() && checkRecruiter_name() && checkRecruiter_phone() && checkUpdteRecruiter_email() && checkUpdteRecruiter_card()){
		return true;
	}else{
		return false;
	}
}

function checkJiu_ps(){
	//判断旧密码
	var jiu_ps = $("#jiu_ps").val();
	if(jiu_ps==""){
		$("#alterContent").text("旧密码不能为空！！！");
		$("#jiu_ps").css("border","1px solid red");
		loadAlter();
		return false;
	}else{
		$("#jiu_ps").css("border",""); 
		return true
	}
}
function checkBasic_password(){
	//判断新密码
	var basic_password = $("#basic_password").val();
	if (basic_password.length < 6 || basic_password.length > 20) {
		// 密码非法,加一个红色边框
		$("#basic_password").css("border", "1px solid red");
		$("#alterContent").html("密码长度不能小于6位，且不能大于20位！！！");
		loadAlter();
		return false;
	} else {
		// 密码合法
		$("#basic_password").css("border", "");
		return true;
	}
}
function checkBasic_password2(){
	//确认新密码
	var basic_password2 = $("#basic_password2").val();
	var basic_password = $("#basic_password").val();
	if(basic_password2!=basic_password){
		$("#alterContent").text("两次密码不一致！！！");
		$("#basic_password").css("border","1px solid red");
		$("#basic_password2").css("border","1px solid red");
		loadAlter();
		return false;
	}else{
		$("#basic_password").css("border","");
		$("#basic_password2").css("border","");
		return true;
	}
}
function checkUpdatePassword(){
	if(checkJiu_ps() && checkBasic_password() && checkBasic_password2()){
		return true;
	}
	return false;
}